package com.inspur.edp.ai.flow.metadata;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.inspur.edp.ai.flow.metadata.serializer.NodesJsonDeserializer;
import com.inspur.edp.ai.flow.spi.GetSubFlowNode;
import lombok.Data;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lizhaorui
 * @date 2025/8/14
 * @description
 */

@Data
public class FlowMetadata {

    private String id;

    private String kind;

    private String code;

    private String name;

    private String version;

    private String description;

    private String parentId;

    private String bizTypeId;

    @JsonDeserialize(using = NodesJsonDeserializer.class)
    private List<AbsNode> nodes;

    private List<Edge> edges;

    private JsonNode extension;

    private boolean sysInit;

    private String createdBy;

    private OffsetDateTime createdOn;

    private String lastChangedBy;

    private OffsetDateTime lastChangedOn;

    private String cacheVersion;

    /**
     * 0 ：未发布、1 : 当前版本未发布  2、已发布
     */
    private String releaseStatus;


    @JsonIgnore
    public List<AbsNode> getAllNodes() {
        List<AbsNode> allNodes = new ArrayList<>();
        if (this.getNodes() != null) {
            for (AbsNode node : this.getNodes()) {
                this.getAllNode(allNodes, node);
            }
        }
        return allNodes;
    }

    @JsonIgnore
    private void getAllNode(List<AbsNode> allNodes, AbsNode node) {
        allNodes.add(node);
        if (node instanceof GetSubFlowNode) {
            List<AbsNode> subNodes = ((GetSubFlowNode) node).getSubFlowNodes();
            if (subNodes != null) {
                for (AbsNode subNode : subNodes) {
                    getAllNode(allNodes, subNode);
                }
            }
        }
    }




}